package com.hj.controller;

import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.hj.domain.dto.CodeDTO;
import com.hj.domain.dto.EncryptedDTO;
import com.hj.domain.dto.JsCodeDTO;
import com.hj.domain.dto.QrcodeDTO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author hj
 * @Date 2023/5/3
 * @Description: 鉴权认证接口
 */

@RestController
@RequestMapping("/wechat")
public class WxLoginController {

    @Resource
    private WxMaService wxMaService;


    /**
     * 登录接口,传参 jsCode
     */
    @PostMapping("/login")
    public WxMaJscode2SessionResult login(@RequestBody JsCodeDTO dto) throws WxErrorException {
        WxMaUserService maUserService = wxMaService.getUserService();
        WxMaJscode2SessionResult sessionInfo = maUserService.getSessionInfo(dto.getJsCode());
        String openid = sessionInfo.getOpenid();
        String sessionKey = sessionInfo.getSessionKey();
        String unionid = sessionInfo.getUnionid();
        // todo  自己的业务逻辑
        return sessionInfo;
    }

    /**
     * 获取手机号  小程序基础库2.22.1以上,需要token
     */
    @PostMapping("/getPhoneNum")
    public String getPhoneNum(@RequestBody CodeDTO dto) throws WxErrorException {
        WxMaUserService maUserService = wxMaService.getUserService();
        WxMaPhoneNumberInfo phoneNoInfo = maUserService.getPhoneNoInfo(dto.getCode());
        String phoneNumber = phoneNoInfo.getPhoneNumber();
        // todo  自己的业务逻辑
        return phoneNumber;
    }

    /**
     * 获取头像和昵称 传入昵称和头像路径,需要token
     */
    @PostMapping("getNickNameAvatar")
    public Boolean getNickNameAvatar(@RequestBody EncryptedDTO dto) {
        WxMaUserService maUserService = wxMaService.getUserService();
        WxMaUserInfo userInfo = maUserService.getUserInfo(
                dto.getSessionKey(), dto.getEncryptedData(), dto.getIvStr()
        );
        String nickName1 = userInfo.getNickName();
        String avatarUrl = userInfo.getAvatarUrl();
        // todo  自己的业务逻辑
        return Boolean.TRUE;
    }


    @PostMapping("getQRCode")
    public void getQrCode(HttpServletResponse response, @RequestBody QrcodeDTO dto) throws WxErrorException, IOException {
        WxMaQrcodeService maQrcodeService = wxMaService.getQrcodeService();
        OutputStream stream;
        byte[] wxaCodeByte = maQrcodeService.createQrcodeBytes(dto.getPath(), dto.getWidth());
        // todo  自己的业务逻辑
        System.out.println(wxaCodeByte.length);
        response.setContentType("image/jpg");
        stream = response.getOutputStream();
        stream.write(wxaCodeByte);
        stream.flush();
        stream.close();
    }


}
